#!/usr/bin/perl

use DBI;
use strict;
use Cwd;

print "[main] remove_processed_files.pl\n\n";

my $dsn="dbi:Pg:dbname=flux_monitoring;host=localhost;port=5432";
my $user="postgres";
my $password="";

# Set up a connection to the database listing the RPFITS files that need to be
# processed.
my $dbh=DBI->connect($dsn,$user,$password,{ RaiseError => 0, AutoCommit => 1});

# query for all observations from the calibration table
my $toc_query="SELECT * FROM calibration";

my $toc=$dbh->prepare($toc_query);
$toc->execute;
my %calibration_row;
my @fields=qw(file source project obstime freq_if1 freq_if2
	      primary1_file primary1_source primary1_project primary1_time
	      primary2_file primary2_source primary2_project primary2_time
	      primary3_file primary3_source primary3_project primary3_time
	      primary4_file primary4_source primary4_project primary4_time
	      primary1_processed primary2_processed primary3_processed primary4_processed
	      bandpass_file bandpass_source bandpass_project bandpass_time);

my @keep_files;
my @remove_files;

while (@calibration_row{@fields}=$toc->fetchrow()){

    print "[main] found source calibrator file '".$calibration_row{'file'}."'\n\n";

    # a file is considered processed and ready to be deleted if:
    #  - there has been a calibration event since the time of the observation
    #  - if a primary calibrator has been assigned to this observation, it has
    #    its processed flag set to true
    #  - if the calibrator is a primary calibrator, if calibration events are
    #    found after the observation time

    # has there been a calibration event since the observation time?
    my $calibration_event_present=0;
    my $ctg_query="SELECT * FROM events WHERE event_time>'".
	$calibration_row{'obstime'}."' AND (if1_freq=".
	$calibration_row{'freq_if1'}." AND if2_freq=".
	$calibration_row{'freq_if2'}.")";
    my $ctg=$dbh->prepare($ctg_query);
    my $ngot=$ctg->execute;
    if ($ngot>0){
	$calibration_event_present=1;
    }
    $ctg->finish;

    if ($calibration_event_present==0){
	# no calibration event found, so we don't think about
	# removing this file yet
	print "[main] not deleting ".$calibration_row{'file'}." as no later ".
	    "calibration event found\n\n";
	for (my $i=1;$i<=4;$i++){
	    if ($calibration_row{"primary$i\_file"} ne ""){
		&add_file_to_list($calibration_row{"primary$i\_file"},0);
		my ($pbandpass_file,$pbandpass_source)=
		    &primary_bandpass($calibration_row{"primary$i\_file"},$dbh);
		if ($pbandpass_file ne ""){
		    &add_file_to_list($pbandpass_file,0);
		}
	    }
	}
	next;
    }

    my $remove_this_file=0;
    
    # check whether it is a primary calibrator
    my $calibrator_is_primary=0;
    my $chp_query="SELECT * FROM calibrators WHERE name='".
	$calibration_row{'source'}."'";
    my $chp=$dbh->prepare($chp_query);
    $chp->execute;
    while (my @calfields=$chp->fetchrow()){
	if ($calfields[3] eq "f"){
	    $calibrator_is_primary=1;
	}
    }
    $chp->finish;
    if ($calibrator_is_primary==0){
	print "[main] calibrator is a phase calibrator\n";
	$remove_this_file=1;
	for (my $i=1;$i<=4;$i++){
	    if (($calibration_row{"primary$i\_file"} ne "")&&
		($calibration_row{"primary$i\_processed"}==0)){
		# source hasn't been processed with this primary, so keep it
		$remove_this_file=0;
	    }
	}
    } else {
	print "[main] calibrator is a flux calibrator\n";
	# remove it
	$remove_this_file=1;
    }

    if ($remove_this_file==1){
	&add_file_to_list($calibration_row{'file'},1);
    } else {
	&add_file_to_list($calibration_row{'file'},0);
	if ($calibration_row{'bandpass_file'} ne ""){
	    &add_file_to_list($calibration_row{'bandpass_file'},0);
	}
	for (my $i=1;$i<=4;$i++){
	    if ($calibration_row{"primary$i\_file"} ne ""){
		&add_file_to_list($calibration_row{"primary$i\_file"},0);
		my ($pbandpass_file,$pbandpass_source)=
		    &primary_bandpass($calibration_row{"primary$i\_file"},$dbh);
		&add_file_to_list($calibration_row{$pbandpass_file},0);
	    }
	}
    }
}
$toc->finish;

for (my $i=0;$i<=$#keep_files;$i++){
    print "[main] keeping file ".$keep_files[$i]."\n";
}
for (my $i=0;$i<=$#remove_files;$i++){
    my $is_protected=0;
    for (my $j=0;$j<=$#keep_files;$j++){
	if ($remove_files[$i] eq $keep_files[$j]){
	    # don't remove this file, it should be kept
	    $is_protected=1;
	    last;
	}
    }
    if ($is_protected==0){
	print "[main] removing file ".$remove_files[$i]."\n";
	&remove_file($remove_files[$i],$dbh);
    }
}



sub remove_file {
    my ($file_to_remove,$dbh)=@_;
    
    my $file_path;
    # query for the file path
    my $qfp_query="SELECT * FROM extracted_files WHERE file='".
	$file_to_remove."'";
    my $qfp=$dbh->prepare($qfp_query);
    $qfp->execute;
    while (my @efres=$qfp->fetchrow()){
	if ($efres[0] eq $file_to_remove){
	    $file_path=$efres[1];
	}
    }
    $qfp->finish;

    my $command="rm -rf ".$file_path."/".$file_to_remove;
    print "[remove_file] $command\n";
    system $command;
}

sub primary_bandpass {
    my ($primary_file,$dbh)=@_;

    my $gbf_query="SELECT bandpass_file,bandpass_source FROM calibration WHERE file='".$primary_file."'";
    my $gbf=$dbh->prepare($gbf_query);
    my $nres=$gbf->execute;
    my $bandpass_file;
    my $bandpass_source;
    if ($nres==1){
	($bandpass_file,$bandpass_source)=$gbf->fetchrow();
    }
    $gbf->finish;

    return ($bandpass_file,$bandpass_source);
}

sub add_file_to_list {
    my ($file_to_add,$list_to_add_to)=@_;

    if ($list_to_add_to==0){
	# add to keep list
	my $add_to_list=1;
	for (my $i=0;$i<=$#keep_files;$i++){
	    if ($file_to_add eq $keep_files[$i]){
		$add_to_list=0;
	    }
	}
	if ($add_to_list==1){
	    push @keep_files,$file_to_add;
	}
    } else {
	# add to remove list
	my $add_to_list=1;
	for (my $i=0;$i<=$#remove_files;$i++){
	    if ($file_to_add eq $remove_files[$i]){
		$add_to_list=0;
	    }
	}
	if ($add_to_list==1){
	    push @remove_files,$file_to_add;
	}
    }
}
